package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVo;
import cn.tedu.csmall.product.pojo.vo.CategoryTreeItemVO;
import cn.tedu.csmall.product.service.CategoryService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/categories")
@Api(tags = "07. 分类管理模块")
@Slf4j
@Validated
public class CategoryController {

    @Autowired
    CategoryService service;

    @PostMapping("/add-new")
    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam){
        service.addNew(categoryAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "类别id",required = true,dataType = "long")
    public JsonResult delete(
            @Range(min = 1,message = "类别id必须大于0")
            @RequestParam Long id){
        log.debug("开始处理【根据ID删除分类】的请求,参数: {}",id);
        service.delete(id);
        return JsonResult.ok();
    }


    @PostMapping("/updateCategory")
    @ApiOperation("修改类别")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "categoryId",value = "类别id",required = true,dataType = "long")
    public JsonResult updateCategory(
            @Range(min = 1,message = "类别id必须大于0")
            @RequestParam Long categoryId, @Valid CategoryUpdateInfoParam categoryUpdateInfoParam){
        service.updateCategory(categoryId,categoryUpdateInfoParam);
        return JsonResult.ok();
    }


    // http://localhost:9180/categories/9527
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【根据ID查询类别详情】的请求，参数：{}", id);
        CategoryStandardVo queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }


    @GetMapping("/list-by-parent")
    @ApiOperation("类别列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "parentId",value = "类别parentId",required = true,dataType = "long")
    public JsonResult list(
            @Range(min = 0,message = "类别parentId必须大于0")
            @RequestParam Long parentId){
        log.debug("开始处理【根据parentId查询类别列表】的请求,参数: {}",parentId);
        List<CategoryListItemVO> list = service.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    // http://localhost:9180/categories/tree
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult listTree() {
        log.debug("开始处理【获取类别树】的业务，参数：无");
        List<CategoryTreeItemVO> categoryTree = service.listTree();
        return JsonResult.ok(categoryTree);
    }
}
